package com.luge.base;

import android.content.Context;
import android.os.Handler;

import com.luge.heart.R;
import com.luge.model.request.BaseModel;
import com.luge.model.request.RequestThread;
import com.luge.model.request.Task;
import com.luge.utils.Helper;

public abstract class BaseNetActivity extends BaseActivity implements
		Handler.Callback {
	protected Handler mHandler = new Handler(this);

	private void addTask(Task task) {
		RequestThread mThread = RequestThread.newInstance(task, mHandler);
		mThread.start();
	}

	protected final void request(int taskID) {
		Helper.showDialog(this);
		Task mTask = new Task(taskID, getUrl(taskID));
		addTask(mTask);
	}

	protected final void request(int taskID, int type) {
		Task mTask = new Task(taskID, getUrl(taskID));
		addTask(mTask);
	}

	protected abstract String getUrl(int taskID);

	protected static boolean preResponse(Context context, BaseModel model) {
		Helper.removeDialog();
		if (model == null) {
			Helper.toast(context, R.string.network_data_no);
			return false;
		}
		if (model.getCode() == 0) {
			Helper.toast(context, model.getErrorInfo());
			return false;
		}
		return true;
	}
}
